package com.huanxing.cloud.mall.api.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.huanxing.cloud.common.core.constant.CacheConstants;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.mall.api.handler.LoginHandler;
import com.huanxing.cloud.mall.api.service.IUserInfoService;
import com.huanxing.cloud.mall.common.constant.MallCommonConstants;
import com.huanxing.cloud.mall.common.dto.UserLoginDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 商城应用授权
 *
 * @author lijx
 * @since 2022/3/10 13:56
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/app")
@Api(value = "app", tags = "应用")
public class AppAuthController {

	private final RedisTemplate<String, String> redisTemplate;

	private final Map<String, LoginHandler> loginHandlerMap;

	private final IUserInfoService userInfoService;

	@ApiOperation(value = "小程序静默登录")
	@PostMapping("/login")
	public Result login(HttpServletRequest request, @RequestBody UserLoginDTO userLoginDTO) {
		String appId = request.getHeader(MallCommonConstants.HEADER_APP_ID);
		userLoginDTO.setAppId(appId);
		String clientType = request.getHeader(MallCommonConstants.HEADER_CLIENT_TYPE);
		userLoginDTO.setClientType(clientType);
		return loginHandlerMap.get(String.format("%s%s", clientType, MallCommonConstants.LOGIN))
			.login(request, userLoginDTO);
	}

	@ApiOperation(value = "短信验证码登录")
	@PostMapping("/login/sms")
	public Result smsLogin(HttpServletRequest request, @RequestBody UserLoginDTO userLoginDTO) {
		String randomStr = userLoginDTO.getPhone();
		String key = CacheConstants.DEFAULT_CODE_KEY + randomStr;
		Object codeObj = redisTemplate.opsForValue().get(key);

		if (ObjectUtil.isEmpty(codeObj) || !userLoginDTO.getCode().equals(codeObj)) {
			throw new RuntimeException("验证码不合法");
		}
		userLoginDTO.setClientType(request.getHeader(MallCommonConstants.HEADER_CLIENT_TYPE));

		return loginHandlerMap.get(MallCommonConstants.SMS_LOGIN).login(request, userLoginDTO);
	}

	@ApiOperation(value = "小程序手机号快速登录")
	@PostMapping("/login/ma/phone")
	public Result maPhoneLogin(HttpServletRequest request, @RequestBody UserLoginDTO userLoginDTO) {
		userLoginDTO.setAppId(request.getHeader(MallCommonConstants.HEADER_APP_ID));
		userLoginDTO.setClientType(request.getHeader(MallCommonConstants.HEADER_CLIENT_TYPE));
		userLoginDTO.setHxToken(request.getHeader(MallCommonConstants.HEADER_HX_TOKEN));
		return loginHandlerMap.get(String.format("%s%s", userLoginDTO.getClientType(), MallCommonConstants.PHONE_LOGIN))
			.login(request, userLoginDTO);
	}

	@ApiOperation(value = "用户密码登录")
	@PostMapping("/login/pwd")
	public Result pwdLogin(HttpServletRequest request, @RequestBody UserLoginDTO userLoginDTO) {
		String clientType = request.getHeader(MallCommonConstants.HEADER_CLIENT_TYPE);
		userLoginDTO.setClientType(clientType);
		userLoginDTO.setHxToken(request.getHeader(MallCommonConstants.HEADER_HX_TOKEN));
		return loginHandlerMap.get(MallCommonConstants.PWD_LOGIN).login(request, userLoginDTO);
	}

	@ApiOperation(value = "退出登录")
	@PostMapping("/logout")
	public Result logout(HttpServletRequest request) {
		return Result.success(userInfoService.logout(request));
	}

}
